package com.huajx.core.annotation;

import java.util.List;
import java.util.Set;

import com.huajx.core.Hboot;
import com.huajx.core.web.HbootWebConfig;
import com.huajx.util.ClassUtil;
import com.huajx.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huajx.core.handler.Controller;
import com.huajx.core.handler.RouteHandler;

public class RouteBundle {
    private final Logger logger = LoggerFactory.getLogger(RouteBundle.class);

    protected static List<String> includeJars = Hboot.config(HbootWebConfig.class).getIncludes();//ConfigKit.getIncludeJars();

    private final static RouteBundle me = new RouteBundle();

    public static RouteBundle me() {
        return me;
    }

    public void scan() {
        Set<Class<?>> controllerClasses = ClassUtil.scanPackageBySuper(null, Controller.class);
//		List<Class<? extends Controller>> controllerClasses = FileUtil.findInClasspathAndJars(Controller.class, includeJars);
        //controllerClasses.addAll(FileKit.findInClasspathAndJars(BaseController.class, includeJars));
        Route route = null;

        //List<String> controllerKeys = new ArrayList<String>();

        for (Class<?> clazz : controllerClasses) {
            Class<Controller> controller = (Class<Controller>) clazz;

            route = (Route) controller.getAnnotation(Route.class);

            if (route == null) {
                continue;
            }

            System.err.println("routes.add==>" + controller.getName());

            StringBuilder sb = new StringBuilder();
            sb.append("routes add ControllerName:").append(controller.getName());

            String[] controllerKeys = route.controllerKey();
            String viewPath = route.viewPath();

            if (controllerKeys == null || controllerKeys.length < 1) {
//			if (StrKit.isBlank(route.controllerKey())) {
                String controllerKey = key(controller, "Controller");
//				if(controllerKeys.contains(controllerKey)){
//					continue;
//				}else{
//					controllerKeys.add(controllerKey);
//				}
                if (StrUtil.isBlank(viewPath)) {
                    RouteHandler.add(controllerKey, controller);
                    sb.append(", controllerKey:").append(controllerKey);
                    //logger.debug("routes.add(" + key(controller, "Controller") + ", " + controller.getName() + ")");
                } else {
                    viewPath = viewPath.trim();

                    RouteHandler.add(controllerKey, controller, viewPath);
                    sb.append(", controllerKey:").append(controllerKey).append(", viewPath:").append(viewPath);
                    //logger.debug("routes.add(" + key(controller, "Controller") + ", " + controller + "," + route.viewPath() + ")");
                }
            } else {

                for (String controllerKey : controllerKeys) {
                    if (StrUtil.isBlank(viewPath)) {
                        RouteHandler.add(controllerKey, controller);
                        sb.append(", controllerKey:").append(controllerKey);
                        //logger.debug("routes.add(" + route.controllerKey() + ", " + controller.getName() + ")");
                    } else {
                        RouteHandler.add(controllerKey, controller, route.viewPath());
                        sb.append(", controllerKey:").append(controllerKey).append(", viewPath:").append(viewPath);
                        //logger.debug("routes.add(" + route.controllerKey() + ", " + controller + "," + route.viewPath() + ")");
                    }

                }
            }
//			if (ConfigKit.getToBoolean(ConstantConfig.CONFIG_DEVELOPE_MODE)) {
//				System.err.println(sb.toString());
//			} else {
//				logger.debug(sb.toString());
//			}
        }
    }

    private String key(Class<?> format, String suffix) {
        String key = StrUtil.lowerFirst(format.getSimpleName());
        key = key.substring(0, key.lastIndexOf(suffix));
        return key;
    }
}
